<template>
  <el-dialog title="会员编辑" :visible.sync="info.isshow">
    <el-form :model="user">
      {{ user }}
      <el-form-item label="手机号" label-width="120px">
        <el-input v-model="user.phone" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="昵称" label-width="120px">
        <el-input v-model="user.nickname" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" label-width="120px">
        <el-input v-model="user.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="状态" label-width="120px">
        <el-switch
          v-model="user.status"
          :active-value="1"
          :inactive-value="2"
        ></el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="update">修 改</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { IndexRoutes } from "../../../router";
import {
  reqmenuadd,
  reqmenudelete,
  reqmemberedit,
  reqmemberinfo,
} from "../../../http/api";
import { successAlert,errorAlert } from "../../../utils/alert";
export default {
  // 4.接收info
  props: ["info", "list"],
  data() {
    return {
      //2.1表单数据
      //   2.2 数据绑定到页面，一一对应到每一个表单
      user: {
        uid:"",
        nickname:"",
        phone:"",
        password:"",
        status: 2,
      },
      //二级路由
      IndexRoutes,
    };
  },
  computed: {
    ...mapGetters({}),
    newList() {
      return this.list.filter((item) => item.id !== this.user.id);
    },
  },
  methods: {
    ...mapActions({}),
    // 5.点了取消
    cancel() {
      this.info.isshow = false;
    },
    //清空user
    empty() {
      this.user = {
        pid: "",
        title: "",
        icon: "",
        type: "",
        url: "",
        status: 2,
      };
    },
    //修改了pid
    changePid() {
      // 如果pid是0，那么type=1;否则，type=2
      if (this.user.pid == 0) {
        this.user.type = 1;
      } else {
        this.user.type = 2;
      }
    },
    //获取一条数据
    getOne(uid) {
      reqmemberinfo({ uid: uid }).then((res) => {
        if (res.data.code == 200) {
          // console.log(res);
          this.user = res.data.list;
          // 补id
          this.user.uid = uid;
        }
      });
    },
    //修改
    update() {
      // 判断密码是否填上
      if(this.user.password == ""){
        errorAlert("密码为必填项");
        return;
      }

      reqmemberedit(this.user).then((res) => {
        // console.log(res);
        if (res.data.code == 200) {
          // 弹成功
          successAlert(res.data.msg);
          // 清空user的数据
          this.empty();
          // 弹框消失
          this.info.isshow = false;
          // 刷新列表
          this.$emit("init");
        }
      });
    },
  },
  mounted() {},
};
</script>
<style lang="less" scoped>
@import "../../../less/index.less";
</style>